package com.zb.jnlxc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.zb.jnlxc.service.DataDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ZLHW.base.Form.Page;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.zb.jnlxc.form.MiniPageReq;
import com.zb.jnlxc.form.MiniPageRsp;
import com.zb.jnlxc.model.Admin;
import com.zb.jnlxc.model.LiaoKuang;
import com.zb.jnlxc.model.PaichanOrderDetail;
import com.zb.jnlxc.model.Truck;
import com.zb.jnlxc.service.PaichanRecordService;
import com.zb.jnlxc.service.ProduceFlowService;

/**
 * Created by zhengbo.zb on 2014/9/17.
 */
@Controller
@RequestMapping("/ProduceFlowAction")
@SessionAttributes(value = { "user" })
public class ProduceFlowAction {
    Logger                       logger = LoggerFactory.getLogger(ProduceFlowAction.class);
    @Resource
    private ProduceFlowService   produceFlowService;
    @Resource
    private PaichanRecordService paichanRecordService;
    @Resource
    private DataDictService dataDictService;

    /**
     * 获取生产任务
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadLiaoKuang")
    public MiniPageRsp loadLiaoKuang(MiniPageReq page, String code, String currentStep,
                                     @ModelAttribute("user") Admin admin) {
        produceFlowService.loadLiaoKuang(page, code, currentStep);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    /**
     * 获取货车
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadTruck")
    public MiniPageRsp loadTruck(MiniPageReq page, String code, @ModelAttribute("user") Admin admin) {
        produceFlowService.loadTruck(page, code);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    /**
     * 搜索可用料库
     * @return
     */
    @ResponseBody
    @RequestMapping("/searchLiaoKuang")
    public List<LiaoKuang> searchLiaoKuang(String key, String currentStep) {
        return produceFlowService.searchLiaoKuang(key, currentStep);
    }

    /**
     * 搜索货车
     * @return
     */
    @ResponseBody
    @RequestMapping("/searchTruck")
    public List<Truck> searchTruck(String key, String currentStep) {
        return produceFlowService.searchTruck(key);
    }

    @ResponseBody
    @RequestMapping("/deleteLiaoKuang")
    private void deleteDepart(int dbId) {
        produceFlowService.deleteLiaoKuang(dbId);
    }

    @ResponseBody
    @RequestMapping("/deleteTruck")
    private void deleteTruck(int dbId) {
        produceFlowService.deleteTruck(dbId);
    }

    @ResponseBody
    @RequestMapping("/eidtLiaoKuang")
    private void eidtDepart(String data) {
        List<LiaoKuang> liaoKuangList = JSONObject.parseObject(data,
            new TypeReference<List<LiaoKuang>>() {
            }.getType());
        for (LiaoKuang liaoKuang : liaoKuangList) {
            produceFlowService.updateLiaoKuang(liaoKuang);
        }
    }

    @ResponseBody
    @RequestMapping("/eidtTruck")
    private void eidtTruck(String data) {
        List<Truck> truckList = JSONObject.parseObject(data, new TypeReference<List<Truck>>() {
        }.getType());
        for (Truck truck : truckList) {
            produceFlowService.updateTruck(truck);
        }
    }

    @ResponseBody
    @RequestMapping("/getQueryModelByLiaoKuang")
    public MiniPageRsp getQueryModelByLiaoKuang(Integer liaoKuangId) {
        return new MiniPageRsp(produceFlowService.getQueryModelByLiaoKuang(liaoKuangId));
    }

    /**
     * 获取机台生产情况（相同图纸未需要使用（未生产）的情况）
     */
    @ResponseBody
    @RequestMapping("/getProductSituation")
    public MiniPageRsp getProductSituation(Page page, Integer mouldId) {
        produceFlowService.getProductSituation(page, mouldId);
        return new MiniPageRsp(page.getResultData(), page.getTotalClum());
    }

    /**
     * 获取工序记录
     */
    @ResponseBody
    @RequestMapping("/getProductProcessByPaiChan")
    public MiniPageRsp getProductProcessByPaiChan(Integer paichanRecordId) {
        return new MiniPageRsp(produceFlowService.getProductProcessByPaiChan(paichanRecordId));
    }

    /**
     * 获取入库记录
     */
    @ResponseBody
    @RequestMapping("/getStorateProcessByOrderId")
    public MiniPageRsp getStorateProcessByOrderId(Integer orderId) {
        return new MiniPageRsp(produceFlowService.getStorateProcessByOrderId(orderId));
    }

    /**
     * 获取料框理论重量
     * @param liaoKuangId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getLiaoKuangWeight")
    public Map<String,Integer> getLiaoKuangWeight(Integer liaoKuangId) {
        int weight = produceFlowService.getLiaoKuangWeight(liaoKuangId);
        int maotiaoWeight = dataDictService.getMaotiaoWeight();
        LiaoKuang liaoKuang = produceFlowService.getLiaoKuang(liaoKuangId);
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("weight",weight);
        map.put("maotiaoWeight",maotiaoWeight);
        map.put("liaokuangWeight",liaoKuang.getWeight());
        return map;
    }

    /**
     * 获取料框理论重量
     * @param yangHuaId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTheoryWeight")
    public float getTheoryWeight(Integer yangHuaId) {
        return produceFlowService.getTheoryWeight(yangHuaId);
    }

    /**
     * 获取料框理论重量
     * @param deliveryId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getDeliveryaWeight")
    public float getDeliveryaWeight(Integer deliveryId) {
        return produceFlowService.getDeliveryaWeight(deliveryId);
    }

    /**
     * 料框挤压过磅
     */
    @ResponseBody
    @RequestMapping("/liaoKuangWeight")
    public void liaoKuangWeight(Integer lastProcessId, String formData,
                                @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            }.getType());
        produceFlowService.liaoKuangWeight(lastProcessId, form, admin);
    }

    /**
     * 料框挤压过磅
     */
    @ResponseBody
    @RequestMapping("/weightFinish")
    public void weightFinish(Integer liaoKuangId, String formData,
                             @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            }.getType());
        produceFlowService.weightFinish(liaoKuangId, form, admin);
    }

    /**
     * 获取料框中待处理的工序
     * @param liaoKuangId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getByLiaoKuang")
    public MiniPageRsp getByLiaoKuang(Integer liaoKuangId) {
        return new MiniPageRsp(produceFlowService.getByLiaoKuang(liaoKuangId));
    }

    @ResponseBody
    @RequestMapping("/getPaichanDetails")
    public List<PaichanOrderDetail> getPaichanDetails(Integer paiChanRecordId) {
        return paichanRecordService.getPaichanDetails(paiChanRecordId);
    }

    @ResponseBody
    @RequestMapping("/shixiao")
    public void shixiao(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.shixiao(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/pengtu")
    public void pengtu(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.pengtu(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/yanghua")
    public void yanghua(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.yanghua(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/pengsha")
    public void pengsha(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.pengsha(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/pack")
    public void pack(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.pack(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/jingju")
    public void jingju(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.jingju(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/deliver")
    public void deliver(Integer lastProcessId, String formData, @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.deliver(lastProcessId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/storageFinish")
    public void storageFinish(Integer liaoKuangId, String formData,
                              @ModelAttribute("user") Admin admin) {
        Map<String, String> form = JSONObject.parseObject(formData,
            new TypeReference<Map<String, String>>() {
            });
        produceFlowService.storageFinish(liaoKuangId, form, admin);
    }

    @ResponseBody
    @RequestMapping("/shixiaoFinish")
    public void shixiaoFinish(Integer liaoKuangId, @ModelAttribute("user") Admin admin) {
        produceFlowService.shixiaoFinish(liaoKuangId, admin);
    }
}
